package com.bba.fms.common.ezspecial.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: fms
 * @Author: 王志鹏
 * @CreateTime: 2019-10-14 09:26
 * @description: 基于poi 简单封装
 */
@Slf4j
public class PoiUtil {
    private PoiUtil() {
    }

    /**
     * 根据Sheet页,读取全部表格内容
     *
     * @param sheet
     * @return
     */
    public static List<List<String>> readWorkbookBySheet(Sheet sheet) {
        List<List<String>> date = new ArrayList<>();
        for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {

            Row hssfRow = sheet.getRow(rowNum);

            List<String> cellDate = new ArrayList<>();
            for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum() - 1; cellNum++) {
                Row row = sheet.getRow(rowNum);
                Cell cell = row.getCell(cellNum);

                if (cell != null) {
                    String clounm = cell.toString();
                    cellDate.add(clounm);
                } else {
                    cellDate.add(null);
                }
            }
            date.add(cellDate);
        }
        return date;
    }

    /**
     * 在指定行最后添加 红色信息
     *
     * @param workbook
     * @param rowNum
     * @param msg
     */
    public static void writeValueByRowLast(Workbook workbook, int rowNum, String msg) {
        Row hssfRow = workbook.getSheetAt(0).getRow(rowNum);

        Cell cell = hssfRow.createCell(hssfRow.getLastCellNum());
        cell.setCellValue(msg);
        cell.setCellStyle(msgStyle(workbook));
    }

    /**
     * 指定单元格标记颜色
     *
     * @param workbook
     * @param r        行
     * @param c        列
     */
    public static void markWarningByCell(Workbook workbook, int r, int c) {
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(r);
        Cell cell = row.getCell(c);
        if (cell == null || cell.toString().equals("")) {
            Cell createCell = row.createCell(c);
            createCell.setCellStyle(createStyle(workbook));
        } else {
            cell.setCellStyle(createStyle(workbook));
        }
    }

    /**
     * 红色样式
     *
     * @param workbook
     * @return
     */
    public static CellStyle msgStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.RED.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        return style;
    }

    /**
     * 黄色样式
     *
     * @param workbook
     * @return
     */
    public static CellStyle createStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        return style;
    }

    /**
     * 通过文件获取 workbook
     * @param file
     * @return
     */
    public static Workbook getWorkBookByFile(MultipartFile file) {
        Workbook workbook = null;
        try (InputStream fileInputStream = file.getInputStream()) {
            workbook = new XSSFWorkbook(fileInputStream);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return workbook;
    }

    /**
     * 当前行标记为空色 并在最后添加错误信息
     * @param workbook
     * @param rowNum
     */
    public static void writeValueByRow(Workbook workbook,int rowNum,String msg){
        Sheet sheet = workbook.getSheetAt(0);
        Row hssfRow = sheet.getRow(rowNum);

        for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum() - 1; cellNum++) {
            Row row = sheet.getRow(rowNum);// 获取第一行
            Cell cell = row.getCell(cellNum);
            if(cell==null){
                cell = row.createCell(cellNum);
            }
            cell.setCellStyle(msgStyle(workbook));
        }

        writeValueByRowLast(workbook,rowNum,msg);
    }

}
